<?php
if (isset($_POST['editer'])) 
{
	?>
	<h1 id="titre">Editer cette catégorie</h1>
	<table border="0" width="100%">
	<?php
	$req = $bdd->prepare('SELECT * FROM categories_textes  WHERE id = :id');
	$req->execute(array('id' => $_POST['id']));
	while ($donnees = $req->fetch())
	{
		?>
		<form method="post" action="index.php?page=admin_categories_textes" enctype="multipart/form-data">
			<tr>
			<td><label for="titre">Titre</label></td>
			<td><input type="text" name="nom" value="<?php echo stripslashes($donnees['nom']); ?>" maxlength="250" size="25"></td>
			</tr>
			<tr><td colspan="2"><label for="description">Description</label></td></tr>
			<tr><td colspan="2"><textarea id="description" name="description" rows="15" cols="80"><?php echo $donnees['description']; ?></textarea></td></tr>
			<tr><td><label for="image">Image</label></td><td><input type="file" size="150" name="image" /></td></tr>
			<tr><td colspan="2"><input type="hidden" name="id" value="<?php echo $donnees['id']; ?>" />
			<input type="submit" value="Modifier" name="modifier" class="bouton" /></td>
			</tr>
		</form>
		<?php
	}
	?>
	</table>
	<?php
	$req->closeCursor();
}
elseif (isset($_POST['modifier'])) 
{
	$req = $bdd->prepare('UPDATE categories_textes SET nom = :nom, description = :description WHERE id=:id');
	$req->execute(array(
		'nom' => stripslashes($_POST['nom']),
		'description' => stripslashes($_POST['description']),
		'id' => $_POST['id']
	));
	if (isset($_FILES['image']) AND $_FILES['image']['error'] == 0)
	{
		if ($_FILES['image']['size'] <= 1000000)
		{
			$infosfichier = pathinfo($_FILES['image']['name']);
			$extension_upload = $infosfichier['extension'];
			$extensions_autorisees = array('jpg', 'jpeg', 'gif', 'png');
			if (in_array($extension_upload, $extensions_autorisees))
			{
				$chemin = "images/textes/".$_POST['id'].'.'.$extension_upload;
				move_uploaded_file($_FILES['image']['tmp_name'], $chemin);
				$req = $bdd->prepare('UPDATE categories_textes SET image=:image WHERE id=:id');
				$req->execute(array(
					'image' => $chemin,
					'id' => $_POST['id']
				));
			}
		}
	}
	header('Location: index.php?page=admin_categories_textes'); 
}
elseif (isset($_POST['effacer'])) 
{
	$req_img = $bdd->prepare('SELECT image FROM categories_textes WHERE id=:id');
	$req_img->execute(array('id' => $_POST['id']));
	$donnees_img = $req_img->fetch();
	$chemin = $donnees_img['image'];
	if (!unlink($chemin)) {
		die('Erreur lors de la destruction de l\'illustration : '.$chemin);
	}

	$req = $bdd->prepare('DELETE FROM categories_textes WHERE id=:id');
	$req->execute(array('id' => $_POST['id']));
	header('Location: index.php?page=admin_categories_textes'); 
}
elseif (isset($_POST['ajouter'])) 
{
	$req = $bdd->prepare('INSERT INTO categories_textes (nom, description) VALUES (:nom, :description)');
	$req->execute(array(
		'nom' => stripslashes($_POST['nom']),
		'description' => stripslashes($_POST['description'])
	));
	header('Location: index.php?page=admin_categories_textes'); 
}
else
{
	?>
	<h1 id="titre">Gestion des catégories</h1>
	<table border="0">
	<?php
	$req = $bdd->query('SELECT * FROM categories_textes ORDER BY nom');
	while ($donnees = $req->fetch())
	{
		?>
		<form method="post" action="index.php?page=admin_categories_textes">
			<tr>
			<td><img src="<?php echo $donnees['image']; ?>" width="120" /></td>
			<td><input type="text" name="nom" value="<?php echo stripslashes($donnees['nom']); ?>" maxlength="250" size="15"></td>
			<td><?php echo stripslashes($donnees['description']); ?>
			<input type="hidden" name="id" value="<?php echo $donnees['id']; ?>" /></td>
			<td><input type="submit" value="Editer" name="editer" class="bouton" />
			<input type="submit" value="Effacer" name="effacer" class="bouton" /></td>
			</tr>
		</form>
		<?php
	}
	$req->closeCursor();
	?>
	<tr><td colspan="4"><hr /></td></tr>
	<form method="post" action="index.php?page=admin_categories_textes">
		<tr>
		<td><input type="text" name="nom" value="" maxlength="250" size="15"></td>
		<td colspan="2"><input type="text" name="description" maxlength="250" size="65"></td>
		<td><input type="submit" value="Ajouter cette catégorie" name="ajouter" class="bouton"/></td>
		</tr>
	</form>
	</table>
<?php
}